package com.ifcar.app.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ifcar.app.db.DBUtils;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.util.StringUtil;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class Videos {
    
    
    public static final String REPLYCOUNT_TAG = "replyCount";
    public static final String DIGEST_TAG = "digest";
    public static final String DIGEST_ALL_TAG = "digestall";
    public static final String TITLE_TAG = "title";
    public static final String HITS_TAG = "hits";
    public static final String COVER_TAG = "cover";
    public static final String REPLYBOARD_TAG = "replyBoard";
    public static final String REPLYID_TAG = "replyid";
    public static final String MP4URL_TAG = "mp4_url";
    public static final String LENGTH_TAG =  "length";
    public static final String VURL_TAG = "vurl";
    public static final String VID_TAG = "vid";
    public static final String PTIME_TAG = "ptime";
    public static final String EX_JSON_TAG = "extendjson";
    public static final String IS_FROM_VIDEO = "isfromvideo";
    
    /**
     * 插入视频列表到数据，仅仅插入最新的视频数据到数据库
     * 参看更多不插入到数据库中
     * @param db
     * @param data
     * @param columnID
     */
    public static void insertListToDB(SQLiteDatabase db, List<HashMap<String, Object>> data, String columnID){
        
        String replyCount = "";
        String digest = "";
        String digestall = "";
        String title = "";
        String hits = "";
        String cover = "";
        String replyBoard = "";
        String replyid = "";
        String mp4_url = "";
        String length = "";
        String vurl = "";
        String vid = "";
        String ptime = "";
        String extendjson = "";
         
        
        try {
            db.beginTransaction();
            
            db.execSQL("delete from " + NewsSQLiteOpenHelper.VIDEO_LIST_TABLE + " where "+NewsSQLiteOpenHelper.VIDEO_COL_ID+"='" + columnID + "'");
            
            if(null!=data){
                
                int size = data.size();
                
                for(int i=0; i<size; i++) {
                    
                    HashMap<String, Object> videoItem = (HashMap<String, Object>)data.get(i);
                    
                    replyCount = getValue(videoItem,REPLYCOUNT_TAG,"0");
                    digest = getValue(videoItem,DIGEST_TAG,"");
                    digestall = getValue(videoItem,DIGEST_ALL_TAG,"");
                    title = getValue(videoItem,TITLE_TAG,"");
                    hits = getValue(videoItem,HITS_TAG,"0");
                    cover = getValue(videoItem,COVER_TAG,"");
                    replyid = getValue(videoItem,REPLYID_TAG,"");
                    mp4_url = getValue(videoItem,MP4URL_TAG,"");
                    length = getValue(videoItem,LENGTH_TAG,"0");
                    vurl = getValue(videoItem,VURL_TAG,"");
                    vid = getValue(videoItem,VID_TAG,"");
                    ptime = getValue(videoItem,PTIME_TAG,"");
                    replyBoard = getValue(videoItem, REPLYBOARD_TAG, "");
                    
                    DBUtils.insertDataToVideoListTable(db, columnID, replyCount, digest, digestall, title, hits, cover, replyBoard, replyid, mp4_url, length, vurl, vid, ptime, extendjson);
                }
                
                
                
            }else{
                
            }
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            db.endTransaction();
        }
    }
    
    public static List<Map<String, Object>> getListFromDB(SQLiteDatabase db, String columnID){
        
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        String sql = "select * from " + NewsSQLiteOpenHelper.VIDEO_LIST_TABLE + " where "
        + NewsSQLiteOpenHelper.VIDEO_COL_ID + "=? limit 0,20";
        Cursor cursor = db.rawQuery(sql, new String[] {columnID});
        
        if (cursor != null && cursor.getCount() != 0) {
            
            HashMap<String, Object> map = null;
            
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                
                map = new HashMap<String, Object>();
                
                map.put(REPLYCOUNT_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_REPLY_COUNT)));
                map.put(DIGEST_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_DIGEST)));
                map.put(DIGEST_ALL_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_DIGEST_ALL)));
                map.put(TITLE_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_TITLE)));
                map.put(HITS_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_HITS)));
                map.put(COVER_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_COVER)));
                map.put(REPLYID_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_REPLY_ID)));
                map.put(MP4URL_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_MP4_URL)));
                map.put(LENGTH_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_LENGTH)));
                map.put(VURL_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_VURL)));
                map.put(VID_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_VID)));
                map.put(PTIME_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_PTIME)));
                map.put(EX_JSON_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_EXTENDS_JSON)));
                map.put(REPLYBOARD_TAG, cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.VIDEO_REPLY_BOARD)));
               
                data.add(map);
            }
            
            cursor.close();
        }
        
        if (cursor != null) {
            cursor.close();
        }
        
        return data;
    }
    
    private static String getValue(HashMap<String, Object> map,String key,String defaultValue){
        
        String rest = defaultValue;
        
        if(null!=map){
            rest =  null==map.get(key)?defaultValue:StringUtil.GetString(map.get(key));
        }else{
            rest =  defaultValue;
        }
        
        return rest;
    }

}
